import logging
from logging.handlers import RotatingFileHandler

from lib.constant.lib_constant import LibConstant
from lib.property.logger_property import LoggerProperty
from lib.utils.print_util import PrintUtil


class LoggerHandler(object):

    root_level = None

    logger = {}

    handler = {}

    handler_type = []

    handler_class = {}

    level = {
        LoggerProperty.LOG_L_INFO: logging.INFO,
        LoggerProperty.LOG_L_DEBUG: logging.DEBUG,
        LoggerProperty.LOG_L_ERROR: logging.ERROR
    }

    @staticmethod
    def init_list_handler(h_list, _type=LoggerProperty.DEFAULT):
        if h_list:
            handler_list = []
            for tmp in h_list:
                h = LoggerHandler.init_handler(tmp.get("l"), tmp.get("p"), tmp.get("f"), tmp.get("d"))
                handler_list.append(h)
            LoggerHandler.handler.setdefault(_type, handler_list)
            LoggerHandler.handler_type.append(_type)

    @staticmethod
    def load_log():
        _log_type = "default"
        if len(LoggerHandler.handler_type) > 0:
            root_level = LoggerHandler.level.get(LoggerHandler.root_level)
            for o in LoggerHandler.handler_type:
                logger = logging.getLogger(o)
                logger.setLevel(root_level)
                _log_type = o

                handler_list = LoggerHandler.handler.get(o)
                for handler in handler_list:
                    logger.addHandler(handler)
                LoggerHandler.logger.setdefault(o, logging.getLogger(o))

        for key in LoggerHandler.handler_class:
            _class = LoggerHandler.handler_class.get(key)
            if _log_type == _class.type:
                _class.logger = LoggerHandler.logger.get(_class.type)
                PrintUtil.start("init log class [%s], overload this object and set logger property." % _class.class_name)

    @staticmethod
    def init_handler(level, file_path, formatter, date_formatter):
        PrintUtil.start("init log handler and the level is %s, file path is %s ..." % (level, file_path))
        handler = None
        if LoggerProperty.LOG_L_INFO == level:
            handler = LoggerHandler.init_info_handler(file_path, formatter, date_formatter)
        elif LoggerProperty.LOG_L_DEBUG == level:
            handler = LoggerHandler.init_debug_handler(file_path, formatter, date_formatter)
        elif LoggerProperty.LOG_L_ERROR == level:
            handler = LoggerHandler.init_error_handler(file_path, formatter, date_formatter)
        return handler

    @staticmethod
    def init_error_handler(file_path, formatter, date_formatter):
        handler = logging.FileHandler(file_path)
        handler.setLevel(logging.ERROR)
        handler.setFormatter(logging.Formatter(formatter, date_formatter))
        return handler

    @staticmethod
    def init_info_handler(file_path, formatter, date_formatter):
        handler = RotatingFileHandler(file_path, maxBytes=10 * 1024 * 1024, backupCount=5, encoding="utf-8")
        handler.setLevel(logging.INFO)
        handler.setFormatter(logging.Formatter(formatter, date_formatter))
        return handler

    @staticmethod
    def init_debug_handler(file_path, formatter, date_formatter):
        handler = RotatingFileHandler(file_path, maxBytes=10 * 1024 * 1024, backupCount=5, encoding="utf-8")
        handler.setLevel(logging.INFO)
        handler.setFormatter(logging.Formatter(formatter, date_formatter))
        return handler
